﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Timers;
using System.Windows.Media;

namespace Mahanet.Abyss.PathCalculation
{
	public class Coordinate2DDataManager
	{
		#region Fields

		private IDataGenerator<double, Coordinate2D> m_DataGenerator;
		private Dictionary<int, IPathOracle<double, Coordinate2D>> m_IdToOracleDictionary
						= new Dictionary<int, IPathOracle<double, Coordinate2D>>();
		private Coordinate2DOracleFactory m_OracleFactory;
		private IAimer<double, Coordinate2D> m_Aimer;

		#endregion

		#region C'tor

		public Coordinate2DDataManager(IDataGenerator<double, Coordinate2D> dataGenerator, Coordinate2DOracleFactory approximatorFactory, IAimer<double, Coordinate2D> aimer)
		{
			m_DataGenerator = dataGenerator;

			m_DataGenerator.DataGenerated += m_DataGenerator_DataGenerated;

			m_OracleFactory = approximatorFactory;

			m_Aimer = aimer;
		}

		#endregion

		#region Public Properties

		public DataPoint<double, Coordinate2D> LatestDataPoint { get; private set; }

		#endregion

		#region Public Methods

		public IPathOracle<double, Coordinate2D> GetApproximator(int dataSeriesUniqueId)
		{
			return m_IdToOracleDictionary[dataSeriesUniqueId];
		}

		#endregion

		#region Event Handlers

		private void m_DataGenerator_DataGenerated(DataPoint<double, Coordinate2D> dataPoint, int dataSeriesId)
		{
			IPathOracle<double, Coordinate2D> approximator;
			if (!m_IdToOracleDictionary.TryGetValue(dataSeriesId, out approximator))
			{
				approximator = m_OracleFactory.Create();
				m_IdToOracleDictionary.Add(dataSeriesId, approximator);
			}

			LatestDataPoint = dataPoint;

			approximator.AddPointData(dataPoint);

			m_Aimer.UpdateApproximator(approximator, dataSeriesId);
		}

		#endregion
	}
}
